package com.tongtong.stsuserapi.controller;

import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.proxy.annotation.AutoGetId;
import com.tongtong.stsuserapi.service.base.NotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags="消息通知接口")
@RestController
@RequestMapping("/sts/notification")
public class NotificationController {

	@Autowired
	NotificationService notificationService;

	@ApiOperation("获取未读消息")
	@GetMapping("/unread")
	@ApiImplicitParam(name = "userid",value = "该参数从token中自动获取，无需传入")
	@AutoGetId
	STSMessage getUnreadNotification(Long userid) {
		return notificationService.getUnreadNotification(userid);
	}

	@ApiOperation("获取已读消息")
	@GetMapping("/read")
	@ApiImplicitParam(name = "userid",value = "该参数从token中自动获取，无需传入")
	@AutoGetId
	STSMessage getReadNotification(Long userid) {
		return notificationService.getReadNotification(userid);
	}

	@ApiOperation("获取已发送消息")
	@GetMapping("/send")
	@ApiImplicitParam(name = "userid",value = "该参数从token中自动获取，无需传入")
	@AutoGetId
	STSMessage getSendNotification(Long userid) {
		return notificationService.getSendNotification(userid);
	}

	@ApiOperation(value = "读取消息",notes = "调用本接口后，消息会由未读转为已读")
	@GetMapping
	@ApiImplicitParam(name = "userid",value = "该参数从token中自动获取，无需传入")
	@AutoGetId
	STSMessage readNotification(Long userid, Long notificationId) {
		return notificationService.readNotification(userid,notificationId);
	}

	@ApiOperation("删除消息")
	@DeleteMapping
	@ApiImplicitParam(name = "userid",value = "该参数从token中自动获取，无需传入")
	@AutoGetId
	STSMessage deleteNotification(Long userid, Long notificationId) {
		return notificationService.deleteNotification(userid,notificationId);
	}

	@ApiOperation("发送消息")
	@PostMapping("/send")
	@PreAuthorize("hasAuthority('manager')")
	@AutoGetId
	@ApiImplicitParams({
			@ApiImplicitParam(name = "senderId",value = "无需传入，若为管理员消息则自动获取，若为系统消息自动设为0"),
			@ApiImplicitParam(name = "noticeType",value = "消息类型(1为系统通知,2为管理员消息)"),
			@ApiImplicitParam(name = "content",value = "该参数使用json格式传输")
	})
	STSMessage sendNotification(Long senderId, Long recipientId, @RequestBody String content, Integer noticeType) {
		return notificationService.sendNotification(senderId,recipientId,content,noticeType);
	}
}
